import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		goods: {},
		order: []
	},
	getters: {
		getGoods: (state) =>state.goods,
		getOrder: (state) =>state.order,
		getAddCount: (state) =>{
			const arr = []
			for (const key in state.goods) {
				state.goods[key].forEach((item,index)=>{
					if (item.count > 0) {
						item.mx = {key, index}
						arr.push(item)
					}
				})
			}
			return arr
		}
	},
	mutations: {
		updataGoods(state, payload) {
			state.goods = payload
		},
		updataGoodsItem(state, { key, index, value }) {
			let objtostr = JSON.stringify(state.goods);
			let newGoods = JSON.parse(objtostr);
			newGoods[key].splice(index, 1, value)
			state.goods = newGoods
		},
		addOrder(state, payload){
			state.order.unshift(payload)
		},
		removeOrder(state, index){
			state.order.splice(index,1)
		}
	},
	actions: {
	},
	modules: {
	}
})
